﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.interfaces;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.datatype;
using System.Web.UI;


namespace Umb.SimpleWebsite.Shared.DataTypes
{
    public class BaseFTypePreValueEditor : PlaceHolder, IDataPrevalue
    {
     #region Fields
        public BaseDataType m_dataType;
        private object m_Locker = new object();//needs to lock control actions

        protected string m_datatypeslist =string.Concat(DBTypes.Date.ToString(),DBTypes.Integer.ToString(),DBTypes.Ntext.ToString(),DBTypes.Nvarchar.ToString());//could be chosen type but now it static
        protected string m_fTName;

        protected TextBox txtPrevalueData;
     #endregion

        public BaseFTypePreValueEditor(BaseDataType dataType, string dataName)
        {
            m_dataType = dataType;
            m_fTName = dataName;
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            BasePropertyType.AddClientDependsObj(m_fTName, this);//general CSS,JavaScripts for FType, FTypeEditor, FTPrevalue, FTLiveEditor
            this.EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            //create just text box witch will be used to save|restore prevalues
            txtPrevalueData = new TextBox() { ID = this.ID + "txtPrevalueData", CssClass = "exdfld_resdata", Text = PreValue.Value };
            this.Controls.Add(txtPrevalueData);
            //show functionality to edit Prevalue data:
            ShowAdditionalFunctionality(txtPrevalueData.ClientID);
        }

        public virtual void ShowAdditionalFunctionality(string TextControlId)
        {
            LiteralControl c = new LiteralControl();
            c.Text = "<!-- [default web functionality. could be override in child class] -->";  
            this.Controls.Add(c);
        }








        private PreValue preValue;
        public virtual PreValue PreValue
        {
            get 
            {
                if (preValue==null){                    
                    var vals = PreValues.GetPreValues(this.m_dataType.DataTypeDefinitionId);                    
                    if (vals.Count >= 1) preValue = (PreValue)vals[0];
                    else  preValue=PreValue.MakeNew(this.m_dataType.DataTypeDefinitionId, Value);
                }
                return preValue;                
            }
            set {
                lock (m_Locker)
                {
                    preValue = value;
                    preValue.Save();
                }                
            }
        }

        public virtual string Value
        {
            get 
            {//return data from controls which edit it
                return txtPrevalueData == null ? "" : txtPrevalueData.Text;
            }
        }

        #region IDataPrevalue Members

        public virtual Control Editor
        {
            get { return this; }
        }

        public virtual void Save()
        {
            string cValue = Value;
            string[] cValues = cValue.Split('|'); 
            if (cValues.Length>1) this.m_dataType.DBType = (DBTypes)Enum.Parse(typeof(DBTypes), cValues[0], true);//get first param
            PreValue curPValue = PreValue;//get current 
            curPValue.Value = Value; //change his value
            PreValue = curPValue;//save in to the dbase
        }
        #endregion


        
        
    }

    
}
